//
//  HZOwnedStockTableViewCell.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/10.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZOwnedStockTableViewCell.h"
#import "NSString+NormalTool.h"

#define kOwnedStockNameFont [UIFont systemFontOfSize:17]
#define kOwnedStockCodeFont [UIFont systemFontOfSize:12]
#define kUpDownMargin 10
#define kLeftRightMargin 10

@interface HZOwnedStockTableViewCell()
@property (nonatomic, strong)UILabel *pNameLabel;
@property (nonatomic, strong)UILabel *pCodeLabel;
@property (nonatomic, strong)UILabel *pPriceLabel;
@property (nonatomic, strong)UILabel *pZhangDieLabel;
@property (nonatomic, strong)UILabel *pZhangFuLabel;
@end

@implementation HZOwnedStockTableViewCell

+ (HZOwnedStockTableViewCell *)ownedStockCellTableView:(UITableView *)tableView
                                             indexPath:(NSIndexPath *)indexPath
                                             cellModel:(HZOwnedStockModel *)cellModel
                                             cellHight:(CGFloat)cellHight
{
    return [self ownedStockCellTableView:tableView indexPath:indexPath cellModel:cellModel cellHight:cellHight cellW:0];
}

+ (HZOwnedStockTableViewCell *)ownedStockCellTableView:(UITableView *)tableView
                                             indexPath:(NSIndexPath *)indexPath
                                             cellModel:(HZOwnedStockModel *)cellModel
                                             cellHight:(CGFloat)cellHight
                                                 cellW:(CGFloat)cellW
{
    return [[self alloc] initWithTableView:tableView indexPath:indexPath cellModel:cellModel cellHight:cellHight cellW:(CGFloat)cellW];
}


- (HZOwnedStockTableViewCell *)initWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath cellModel:(HZOwnedStockModel *)cellModel cellHight:(CGFloat)cellHight cellW:(CGFloat)cellW
{
    static NSString *ID = @"ownedStockCell";
    HZOwnedStockTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[HZOwnedStockTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell p_commonInit];
    }
    [cell p_updateDataWithmodel:cellModel indexPath:indexPath cellH:cellHight cellW:cellW];
    
    return cell;
}

- (void)p_commonInit
{
    _pNameLabel = [UILabel new];
    _pCodeLabel = [UILabel new];
    _pPriceLabel = [UILabel new];
    _pZhangFuLabel = [UILabel new];
    _pZhangDieLabel = [UILabel new];
    _pNameLabel.font = kOwnedStockNameFont;
    _pCodeLabel.font = kOwnedStockCodeFont;
    _pPriceLabel.textAlignment = _pZhangDieLabel.textAlignment = NSTextAlignmentCenter;
    _pZhangFuLabel.textAlignment = NSTextAlignmentCenter;
    _pNameLabel.textAlignment = _pCodeLabel.textAlignment = NSTextAlignmentLeft;
    
    _pPriceLabel.font = _pZhangDieLabel.font = _pZhangFuLabel.font = [UIFont systemFontOfSize:20];
    _pZhangFuLabel.textColor = [UIColor whiteColor];
    _pCodeLabel.textColor = kMYHexToRGB(0x999999, 1);
    
    
    [self.contentView addSubview:_pNameLabel];
    [self.contentView addSubview:_pCodeLabel];
    [self.contentView addSubview:_pPriceLabel];
    [self.contentView addSubview:_pZhangFuLabel];
    [self.contentView addSubview:_pZhangDieLabel];
}

- (void)p_updateDataWithmodel:(HZOwnedStockModel *)model indexPath:(NSIndexPath *)indexPath cellH:(CGFloat)cellH cellW:(CGFloat)cellW
{
    [self p_cleaOldData];
    [self p_updateNewData:model];
    [self p_updateFrame:cellH cellW:cellW];
}

- (void)p_updateNewData:(HZOwnedStockModel *)model
{
    _pNameLabel.text = model.model_Name;
    _pCodeLabel.text = model.model_Code;
    _pPriceLabel.text = model.model_Price;
    _pZhangFuLabel.text = model.model_ZhangFu;
    _pZhangDieLabel.text = model.model_ZhangDie;
    _pPriceLabel.textColor = _pZhangDieLabel.textColor = model.model_Color;
    
    _pZhangFuLabel.backgroundColor = kMYGetZhangDieColor([model.model_ZhangDie floatValue], 0);
}

- (void)p_updateFrame:(CGFloat)cellH cellW:(CGFloat)cellW
{
    if (cellW == 0) {
        cellW = kScreenW;
    }
    CGFloat fPartW = (cellW - 2*kLeftRightMargin)/4;
    CGSize sNameSize = [_pNameLabel.text sizeWithLimitedWidth:MAXFLOAT withFont:kOwnedStockNameFont];
    CGSize sCodeSize = [_pCodeLabel.text sizeWithLimitedWidth:MAXFLOAT withFont:kOwnedStockCodeFont];
    
    _pNameLabel.frame = CGRectMake(kLeftRightMargin, kUpDownMargin, fPartW, sNameSize.height);
    _pCodeLabel.frame = CGRectMake(kLeftRightMargin, cellH - kUpDownMargin - sCodeSize.height, fPartW, sCodeSize.height);
    _pPriceLabel.frame = CGRectMake(CGRectGetMaxX(_pNameLabel.frame), 0, fPartW, cellH);
    _pZhangDieLabel.frame = CGRectMake(CGRectGetMaxX(_pPriceLabel.frame), 0, fPartW, cellH);
    
    CGFloat zhangfuW = fPartW - 10;
    _pZhangFuLabel.frame = CGRectMake(cellW - kLeftRightMargin - zhangfuW, 12.5, zhangfuW, 35);
    _pZhangFuLabel.layer.cornerRadius = 35/2;
    _pZhangFuLabel.layer.masksToBounds = YES;
}

- (void)p_cleaOldData
{
    [_pNameLabel setText:@""];
    [_pCodeLabel setText:@""];
    [_pPriceLabel setText:@""];
    [_pZhangFuLabel setText:@""];
    [_pZhangDieLabel setText:@""];

}

@end
